define(function(require, exports, module) {
	'use strict';
    

    var rk_userService = {
        DEFAULT_USER_ICON: window.DEFAULTS.USER_DEFAULT_ICON
    };

    var currentPageUserId = SESSION.user.id;

    var userInfo = {};
    if (window.SESSION && window.SESSION.user) {
        var currentId = window.SESSION.user.id;
        userInfo[currentId + ''] = $.extend(true, {}, window.SESSION.user);
    }
    rk_userService.loadUser = function(data) {
        if (!data.data || !data.data.users) return;
        rk_userService.appendUserInfo(data.data.users);
        var currentId = currentPageUserId;
        var currentInfo = rk_userService.getUserInfo(currentId);
        if (!currentInfo && window.SESSION && window.SESSION.user) {
            currentId = window.SESSION.user.id;
            userInfo[currentId] = $.extend(true, {}, window.SESSION.user);
        }
    };
    var recentUserList = [];
    rk_userService.loadRecentUserList = function(data) {
        if (!data || !data.data) return;
        if (data.data.recentUserList) {
            recentUserList = data.data.recentUserList;
            for(var i = 0; i < recentUserList.length; i++){
                var user = recentUserList[i];
                appendUserInfo(user.id, user);
            }
        }
    };
    rk_userService.getRecentUserList = function() {
        return recentUserList;
    };
    rk_userService.updateRecentUserList = function(user) {
        if (currentPageUserId === user.id) return; //can not add myself
        if (recentUserList.length == 0) {
            recentUserList = [user];
            return recentUserList;
        }
        var id = user.id;
        user = $.extend(true, {}, user);
        var newList = [user];
        for (var i = 0, len = recentUserList.length; i < len; i++) {
            if (id != recentUserList[i].id) {
                var u = $.extend(true, {}, recentUserList[i]);
                newList.push(u);
            }
        }
        recentUserList = newList;
        return recentUserList;
    };
    rk_userService.appendUserInfo = function(json) {
        if (!json) return;
        if ($.isArray(json)) {
            for (var i = 0; i < json.length; i++) {
                var user = json[i];
                var id = user.id + '';
                appendUserInfo(id, user);
            }
        } else {
            for (id in json) {
                appendUserInfo(id, json[id]);
            }
        }
    };
    var appendUserInfo = function(id, user) {
        if (userInfo[id]) {
            $.extend(true, userInfo[id], user);
        } else {
            userInfo[id] = user;
        }
    };
    rk_userService.hasUser = function(id) {
        return !!userInfo[id];
    };
    rk_userService.getUserInfo = function(id) {
        id = id + '';
        var info = userInfo[id];
        var ctxPath = '';
        if (info) {
            if (!info.icon) info.icon = rk_userService.DEFAULT_USER_ICON;
            return info;
        } else if (id) { //有id，但users中没过来信息
            return {
                icon: rk_userService.DEFAULT_USER_ICON
            };
        }
    };
    rk_userService.getDefaultFace = function() {
        var ctxPath = '';
        return ctxPath + rk_userService.DEFAULT_USER_ICON;
    };
    module.exports = rk_userService;
});
